namespace Avalonia.Input;

/// <summary>
/// Contains extension methods related to <see cref="PhysicalKey"/>.
/// </summary>
public static class PhysicalKeyExtensions
{
    /// <summary>
    /// Maps a physical key to a corresponding key, if possible, on a QWERTY keyboard.
    /// </summary>
    /// <param name="physicalKey">the physical key to map.</param>
    /// <returns>The key corresponding to <paramref name="physicalKey"/>, or <see cref="Key.None"/>.</returns>
    public static Key ToQwertyKey(this PhysicalKey physicalKey)
        => physicalKey switch
        {
            PhysicalKey.None => Key.None,

            // Writing System Keys
            PhysicalKey.Backquote => Key.Oem3,
            PhysicalKey.Backslash => Key.Oem5,
            PhysicalKey.BracketLeft => Key.Oem4,
            PhysicalKey.BracketRight => Key.Oem6,
            PhysicalKey.Comma => Key.OemComma,
            PhysicalKey.Digit0 => Key.D0,
            PhysicalKey.Digit1 => Key.D1,
            PhysicalKey.Digit2 => Key.D2,
            PhysicalKey.Digit3 => Key.D3,
            PhysicalKey.Digit4 => Key.D4,
            PhysicalKey.Digit5 => Key.D5,
            PhysicalKey.Digit6 => Key.D6,
            PhysicalKey.Digit7 => Key.D7,
            PhysicalKey.Digit8 => Key.D8,
            PhysicalKey.Digit9 => Key.D9,
            PhysicalKey.Equal => Key.OemPlus,
            PhysicalKey.IntlBackslash => Key.Oem102,
            PhysicalKey.IntlRo => Key.Oem102,
            PhysicalKey.IntlYen => Key.Oem5,
            PhysicalKey.A => Key.A,
            PhysicalKey.B => Key.B,
            PhysicalKey.C => Key.C,
            PhysicalKey.D => Key.D,
            PhysicalKey.E => Key.E,
            PhysicalKey.F => Key.F,
            PhysicalKey.G => Key.G,
            PhysicalKey.H => Key.H,
            PhysicalKey.I => Key.I,
            PhysicalKey.J => Key.J,
            PhysicalKey.K => Key.K,
            PhysicalKey.L => Key.L,
            PhysicalKey.M => Key.M,
            PhysicalKey.N => Key.N,
            PhysicalKey.O => Key.O,
            PhysicalKey.P => Key.P,
            PhysicalKey.Q => Key.Q,
            PhysicalKey.R => Key.R,
            PhysicalKey.S => Key.S,
            PhysicalKey.T => Key.T,
            PhysicalKey.U => Key.U,
            PhysicalKey.V => Key.V,
            PhysicalKey.W => Key.W,
            PhysicalKey.X => Key.X,
            PhysicalKey.Y => Key.Y,
            PhysicalKey.Z => Key.Z,
            PhysicalKey.Minus => Key.OemMinus,
            PhysicalKey.Period => Key.OemPeriod,
            PhysicalKey.Quote => Key.Oem7,
            PhysicalKey.Semicolon => Key.Oem1,
            PhysicalKey.Slash => Key.Oem2,

            // Functional Keys
            PhysicalKey.AltLeft => Key.LeftAlt,
            PhysicalKey.AltRight => Key.RightAlt,
            PhysicalKey.Backspace => Key.Back,
            PhysicalKey.CapsLock => Key.CapsLock,
            PhysicalKey.ContextMenu => Key.Apps,
            PhysicalKey.ControlLeft => Key.LeftCtrl,
            PhysicalKey.ControlRight => Key.RightCtrl,
            PhysicalKey.Enter => Key.Enter,
            PhysicalKey.MetaLeft => Key.LWin,
            PhysicalKey.MetaRight => Key.RWin,
            PhysicalKey.ShiftLeft => Key.LeftShift,
            PhysicalKey.ShiftRight => Key.RightShift,
            PhysicalKey.Space => Key.Space,
            PhysicalKey.Tab => Key.Tab,
            PhysicalKey.Convert => Key.ImeConvert,
            PhysicalKey.KanaMode => Key.KanaMode,
            PhysicalKey.Lang1 => Key.HangulMode,
            PhysicalKey.Lang2 => Key.HanjaMode,
            PhysicalKey.Lang3 => Key.DbeKatakana,
            PhysicalKey.Lang4 => Key.DbeHiragana,
            PhysicalKey.Lang5 => Key.OemAuto,
            PhysicalKey.NonConvert => Key.ImeNonConvert,

            // Control Pad Section
            PhysicalKey.Delete => Key.Delete,
            PhysicalKey.End => Key.End,
            PhysicalKey.Help => Key.Help,
            PhysicalKey.Home => Key.Home,
            PhysicalKey.Insert => Key.Insert,
            PhysicalKey.PageDown => Key.PageDown,
            PhysicalKey.PageUp => Key.PageUp,

            // Arrow Pad Section
            PhysicalKey.ArrowDown => Key.Down,
            PhysicalKey.ArrowLeft => Key.Left,
            PhysicalKey.ArrowRight => Key.Right,
            PhysicalKey.ArrowUp => Key.Up,

            // Numpad Section
            PhysicalKey.NumLock => Key.NumLock,
            PhysicalKey.NumPad0 => Key.NumPad0,
            PhysicalKey.NumPad1 => Key.NumPad1,
            PhysicalKey.NumPad2 => Key.NumPad2,
            PhysicalKey.NumPad3 => Key.NumPad3,
            PhysicalKey.NumPad4 => Key.NumPad4,
            PhysicalKey.NumPad5 => Key.NumPad5,
            PhysicalKey.NumPad6 => Key.NumPad6,
            PhysicalKey.NumPad7 => Key.NumPad7,
            PhysicalKey.NumPad8 => Key.NumPad8,
            PhysicalKey.NumPad9 => Key.NumPad9,
            PhysicalKey.NumPadAdd => Key.Add,
            PhysicalKey.NumPadClear => Key.Clear,
            PhysicalKey.NumPadComma => Key.AbntC2,
            PhysicalKey.NumPadDecimal => Key.Decimal,
            PhysicalKey.NumPadDivide => Key.Divide,
            PhysicalKey.NumPadEnter => Key.Enter,
            PhysicalKey.NumPadEqual => Key.OemPlus,
            PhysicalKey.NumPadMultiply => Key.Multiply,
            PhysicalKey.NumPadParenLeft => Key.Oem4,
            PhysicalKey.NumPadParenRight => Key.Oem6,
            PhysicalKey.NumPadSubtract => Key.Subtract,

            // Function Section
            PhysicalKey.Escape => Key.Escape,
            PhysicalKey.F1 => Key.F1,
            PhysicalKey.F2 => Key.F2,
            PhysicalKey.F3 => Key.F3,
            PhysicalKey.F4 => Key.F4,
            PhysicalKey.F5 => Key.F5,
            PhysicalKey.F6 => Key.F6,
            PhysicalKey.F7 => Key.F7,
            PhysicalKey.F8 => Key.F8,
            PhysicalKey.F9 => Key.F9,
            PhysicalKey.F10 => Key.F10,
            PhysicalKey.F11 => Key.F11,
            PhysicalKey.F12 => Key.F12,
            PhysicalKey.F13 => Key.F13,
            PhysicalKey.F14 => Key.F14,
            PhysicalKey.F15 => Key.F15,
            PhysicalKey.F16 => Key.F16,
            PhysicalKey.F17 => Key.F17,
            PhysicalKey.F18 => Key.F18,
            PhysicalKey.F19 => Key.F19,
            PhysicalKey.F20 => Key.F20,
            PhysicalKey.F21 => Key.F21,
            PhysicalKey.F22 => Key.F22,
            PhysicalKey.F23 => Key.F23,
            PhysicalKey.F24 => Key.F24,
            PhysicalKey.PrintScreen => Key.PrintScreen,
            PhysicalKey.ScrollLock => Key.Scroll,
            PhysicalKey.Pause => Key.Pause,

            // Media Keys
            PhysicalKey.BrowserBack => Key.BrowserBack,
            PhysicalKey.BrowserFavorites => Key.BrowserFavorites,
            PhysicalKey.BrowserForward => Key.BrowserForward,
            PhysicalKey.BrowserHome => Key.BrowserHome,
            PhysicalKey.BrowserRefresh => Key.BrowserRefresh,
            PhysicalKey.BrowserSearch => Key.BrowserSearch,
            PhysicalKey.BrowserStop => Key.BrowserStop,
            PhysicalKey.Eject => Key.None,
            PhysicalKey.LaunchApp1 => Key.LaunchApplication1,
            PhysicalKey.LaunchApp2 => Key.LaunchApplication2,
            PhysicalKey.LaunchMail => Key.LaunchMail,
            PhysicalKey.MediaPlayPause => Key.MediaPlayPause,
            PhysicalKey.MediaSelect => Key.SelectMedia,
            PhysicalKey.MediaStop => Key.MediaStop,
            PhysicalKey.MediaTrackNext => Key.MediaNextTrack,
            PhysicalKey.MediaTrackPrevious => Key.MediaPreviousTrack,
            PhysicalKey.Power => Key.None,
            PhysicalKey.Sleep => Key.Sleep,
            PhysicalKey.AudioVolumeDown => Key.VolumeDown,
            PhysicalKey.AudioVolumeMute => Key.VolumeMute,
            PhysicalKey.AudioVolumeUp => Key.VolumeUp,
            PhysicalKey.WakeUp => Key.None,

            // Legacy Keys
            PhysicalKey.Again => Key.None,
            PhysicalKey.Copy => Key.OemCopy,
            PhysicalKey.Cut => Key.None,
            PhysicalKey.Find => Key.None,
            PhysicalKey.Open => Key.None,
            PhysicalKey.Paste => Key.None,
            PhysicalKey.Props => Key.None,
            PhysicalKey.Select => Key.Select,
            PhysicalKey.Undo => Key.None,

            _ => Key.None
        };

    /// <summary>
    /// Maps a physical key to a corresponding key symbol, if possible, on a QWERTY keyboard.
    /// </summary>
    /// <param name="physicalKey">the physical key to map.</param>
    /// <param name="useShiftModifier">Indicates whether the Shift key is considered pressed.</param>
    /// <returns>The key corresponding to <paramref name="physicalKey"/>, or <see cref="Key.None"/>.</returns>
    public static string? ToQwertyKeySymbol(this PhysicalKey physicalKey, bool useShiftModifier = false)
        => physicalKey switch
        {
            PhysicalKey.None => null,

            // Writing System Keys
            PhysicalKey.Backquote => useShiftModifier ? "~" : "`",
            PhysicalKey.Backslash => useShiftModifier ? "|" : "\\",
            PhysicalKey.BracketLeft => useShiftModifier ? "{" : "[",
            PhysicalKey.BracketRight => useShiftModifier ? "}" : "]",
            PhysicalKey.Comma =>  useShiftModifier ? "<" : ",",
            PhysicalKey.Digit0 => useShiftModifier ? ")" : "0",
            PhysicalKey.Digit1 => useShiftModifier ? "!" : "1",
            PhysicalKey.Digit2 => useShiftModifier ? "@" : "2",
            PhysicalKey.Digit3 => useShiftModifier ? "#" : "3",
            PhysicalKey.Digit4 => useShiftModifier ? "$" : "4",
            PhysicalKey.Digit5 => useShiftModifier ? "%" : "5",
            PhysicalKey.Digit6 => useShiftModifier ? "^" : "6",
            PhysicalKey.Digit7 => useShiftModifier ? "&" : "7",
            PhysicalKey.Digit8 => useShiftModifier ? "*" : "8",
            PhysicalKey.Digit9 => useShiftModifier ? "(" : "9",
            PhysicalKey.Equal => useShiftModifier ? "+" : "=",
            PhysicalKey.IntlBackslash => useShiftModifier ? "|" : "\\",
            PhysicalKey.IntlRo => null,
            PhysicalKey.IntlYen => null,
            PhysicalKey.A => useShiftModifier ? "A" : "a",
            PhysicalKey.B => useShiftModifier ? "B" : "b",
            PhysicalKey.C => useShiftModifier ? "C" : "c",
            PhysicalKey.D => useShiftModifier ? "D" : "d",
            PhysicalKey.E => useShiftModifier ? "E" : "e",
            PhysicalKey.F => useShiftModifier ? "F" : "f",
            PhysicalKey.G => useShiftModifier ? "G" : "g",
            PhysicalKey.H => useShiftModifier ? "H" : "h",
            PhysicalKey.I => useShiftModifier ? "I" : "i",
            PhysicalKey.J => useShiftModifier ? "J" : "j",
            PhysicalKey.K => useShiftModifier ? "K" : "k",
            PhysicalKey.L => useShiftModifier ? "L" : "l",
            PhysicalKey.M => useShiftModifier ? "M" : "m",
            PhysicalKey.N => useShiftModifier ? "N" : "n",
            PhysicalKey.O => useShiftModifier ? "O" : "o",
            PhysicalKey.P => useShiftModifier ? "P" : "p",
            PhysicalKey.Q => useShiftModifier ? "Q" : "q",
            PhysicalKey.R => useShiftModifier ? "R" : "r",
            PhysicalKey.S => useShiftModifier ? "S" : "s",
            PhysicalKey.T => useShiftModifier ? "T" : "t",
            PhysicalKey.U => useShiftModifier ? "U" : "u",
            PhysicalKey.V => useShiftModifier ? "V" : "v",
            PhysicalKey.W => useShiftModifier ? "W" : "w",
            PhysicalKey.X => useShiftModifier ? "X" : "x",
            PhysicalKey.Y => useShiftModifier ? "Y" : "y",
            PhysicalKey.Z => useShiftModifier ? "Z" : "z",
            PhysicalKey.Minus => useShiftModifier ? "_" : "-",
            PhysicalKey.Period => useShiftModifier ? ">" : ".",
            PhysicalKey.Quote => useShiftModifier ? "\"" : "'",
            PhysicalKey.Semicolon => useShiftModifier ? ":" : ";",
            PhysicalKey.Slash => useShiftModifier ? "?" : "/",

            // Functional Keys
            PhysicalKey.AltLeft => null,
            PhysicalKey.AltRight => null,
            PhysicalKey.Backspace => "\b",
            PhysicalKey.CapsLock => null,
            PhysicalKey.ContextMenu => null,
            PhysicalKey.ControlLeft => null,
            PhysicalKey.ControlRight => null,
            PhysicalKey.Enter => "\r",
            PhysicalKey.MetaLeft => null,
            PhysicalKey.MetaRight => null,
            PhysicalKey.ShiftLeft => null,
            PhysicalKey.ShiftRight => null,
            PhysicalKey.Space => " ",
            PhysicalKey.Tab => "\t",
            PhysicalKey.Convert => null,
            PhysicalKey.KanaMode => null,
            PhysicalKey.Lang1 => null,
            PhysicalKey.Lang2 => null,
            PhysicalKey.Lang3 => null,
            PhysicalKey.Lang4 => null,
            PhysicalKey.Lang5 => null,
            PhysicalKey.NonConvert => null,

            // Control Pad Section
            PhysicalKey.Delete => null,
            PhysicalKey.End => null,
            PhysicalKey.Help => null,
            PhysicalKey.Home => null,
            PhysicalKey.Insert => null,
            PhysicalKey.PageDown => null,
            PhysicalKey.PageUp => null,

            // Arrow Pad Section
            PhysicalKey.ArrowDown => null,
            PhysicalKey.ArrowLeft => null,
            PhysicalKey.ArrowRight => null,
            PhysicalKey.ArrowUp => null,

            // Numpad Section
            PhysicalKey.NumLock => null,
            PhysicalKey.NumPad0 => "0",
            PhysicalKey.NumPad1 => "1",
            PhysicalKey.NumPad2 => "2",
            PhysicalKey.NumPad3 => "3",
            PhysicalKey.NumPad4 => "4",
            PhysicalKey.NumPad5 => "5",
            PhysicalKey.NumPad6 => "6",
            PhysicalKey.NumPad7 => "7",
            PhysicalKey.NumPad8 => "8",
            PhysicalKey.NumPad9 => "9",
            PhysicalKey.NumPadAdd => "+",
            PhysicalKey.NumPadClear => null,
            PhysicalKey.NumPadComma => ",",
            PhysicalKey.NumPadDecimal => ".",
            PhysicalKey.NumPadDivide => "/",
            PhysicalKey.NumPadEnter => "\r",
            PhysicalKey.NumPadEqual => "=",
            PhysicalKey.NumPadMultiply => "*",
            PhysicalKey.NumPadParenLeft => "(",
            PhysicalKey.NumPadParenRight => ")",
            PhysicalKey.NumPadSubtract => "-",

            // Function Section
            PhysicalKey.Escape => "\u001B",
            PhysicalKey.F1 => null,
            PhysicalKey.F2 => null,
            PhysicalKey.F3 => null,
            PhysicalKey.F4 => null,
            PhysicalKey.F5 => null,
            PhysicalKey.F6 => null,
            PhysicalKey.F7 => null,
            PhysicalKey.F8 => null,
            PhysicalKey.F9 => null,
            PhysicalKey.F10 => null,
            PhysicalKey.F11 => null,
            PhysicalKey.F12 => null,
            PhysicalKey.F13 => null,
            PhysicalKey.F14 => null,
            PhysicalKey.F15 => null,
            PhysicalKey.F16 => null,
            PhysicalKey.F17 => null,
            PhysicalKey.F18 => null,
            PhysicalKey.F19 => null,
            PhysicalKey.F20 => null,
            PhysicalKey.F21 => null,
            PhysicalKey.F22 => null,
            PhysicalKey.F23 => null,
            PhysicalKey.F24 => null,
            PhysicalKey.PrintScreen => null,
            PhysicalKey.ScrollLock => null,
            PhysicalKey.Pause => null,

            // Media Keys
            PhysicalKey.BrowserBack => null,
            PhysicalKey.BrowserFavorites => null,
            PhysicalKey.BrowserForward => null,
            PhysicalKey.BrowserHome => null,
            PhysicalKey.BrowserRefresh => null,
            PhysicalKey.BrowserSearch => null,
            PhysicalKey.BrowserStop => null,
            PhysicalKey.Eject => null,
            PhysicalKey.LaunchApp1 => null,
            PhysicalKey.LaunchApp2 => null,
            PhysicalKey.LaunchMail => null,
            PhysicalKey.MediaPlayPause => null,
            PhysicalKey.MediaSelect => null,
            PhysicalKey.MediaStop => null,
            PhysicalKey.MediaTrackNext => null,
            PhysicalKey.MediaTrackPrevious => null,
            PhysicalKey.Power => null,
            PhysicalKey.Sleep => null,
            PhysicalKey.AudioVolumeDown => null,
            PhysicalKey.AudioVolumeMute => null,
            PhysicalKey.AudioVolumeUp => null,
            PhysicalKey.WakeUp => null,

            // Legacy Keys
            PhysicalKey.Again => null,
            PhysicalKey.Copy => null,
            PhysicalKey.Cut => null,
            PhysicalKey.Find => null,
            PhysicalKey.Open => null,
            PhysicalKey.Paste => null,
            PhysicalKey.Props => null,
            PhysicalKey.Select => null,
            PhysicalKey.Undo => null,

            _ => null
        };
}
